package com.bilibili.juc.LockSupport;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

@Slf4j(topic = "c.LockSupportDemo2")
public class LockSupportDemo2 {
    public static void main(String[] args) {
//        testThreadInterrupted();
        testThreadNormalTerminate();
        log.debug(" main method running!!!");
    }

    private static void testThreadNormalTerminate() {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            if (Thread.currentThread().isInterrupted()) {
                log.debug(" is interrupted");
            } else {
                log.debug("normal terminate");
            }
        }, "t1");
        t1.start();
        Thread t2 = new Thread(() -> {
            LockSupport.unpark(t1);
        }, "t2");
        t2.start();
/*
10:33:01.571 c.LockSupportDemo2 [main] -  main method running!!!
10:33:01.571 c.LockSupportDemo2 [t1] - normal terminate
 */
    }

    private static void testThreadInterrupted() {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            if (Thread.currentThread().isInterrupted()) {
                log.debug(" is interrupted");
            } else {
                log.debug("normal terminate");
            }
        }, "t1");
        t1.start();
        Thread t2 = new Thread(() -> {
            t1.interrupt();
        }, "t2");
        t2.start();
/*
10:30:11.169 c.LockSupportDemo2 [main] -  main method running!!!
10:30:11.169 c.LockSupportDemo2 [t1] -  is interrupted
 */
    }
}
